package com.cyj.mongodbcomment.cyj.controller;

import com.cyj.mongodbcomment.cyj.pojo.Comment;
import com.cyj.mongodbcomment.cyj.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/findAll")
    public List<Comment> findAll(){
        List<Comment> commentList = commentService.findAll();
        return commentList;
    }

    @GetMapping("/save")
    public Map save(Comment comment, String articleid){
        Map<String,Object> map = new HashMap<String,Object>();
        Comment comment1 = commentService.save(comment,articleid);
        if (comment1==null){
            map.put("msg","添加失败");
        }else{
            map.put("msg","添加成功");
        }

        return map;
    }

    @GetMapping("/update")
    public Map update(Comment comment){
        Map<String,Object> map = new HashMap<String,Object>();
        Comment comment1 = commentService.update(comment);
        if (comment1==null){
            map.put("msg","修改失败");
        }else{
            map.put("msg","修改成功");
        }

        return map;
    }

    @GetMapping("/deleteById")
    public Map deleteById(String id){
        Map<String,Object> map = new HashMap<String,Object>();
        commentService.deleteById(id);
        map.put("msg","删除成功");
        return map;
    }

}
